<template>
    <a-card title="操作员管理" :bordered="false">
        <!-- Card Header -->


        <template slot="extra">
            <a-form layout="inline" @submit.prevent="searchSubmit">
                <a-form-item>
                    <a-input v-model="searchForm.name" placeholder="操作员姓名"></a-input>
                </a-form-item>
                <a-form-item>
                    <a-input v-model="searchForm.account" placeholder="登录账号"></a-input>
                </a-form-item>
                <a-form-item>
                    <a-input v-model="searchForm.phone" placeholder="手机号码"></a-input>
                </a-form-item>
                <a-form-item>
                    <a-input v-model="searchForm.email" placeholder="电子邮箱"></a-input>
                </a-form-item>
                <a-form-item>
                    <a-select show-search placeholder="状态" allowClear showSearch v-model="searchForm.status"
                              option-filter-prop="children"
                              style="width: 150px">
                        <a-select-option v-for="(item, index) in OperatorStatusOPT" :key="index" :value="item.value">{{
                            item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-item>
                <a-form-item>
                    <a-button icon="search" type="primary" html-type="submit">搜索</a-button>
                </a-form-item>
                <a-form-item>
                    <a-button icon="plus" type="danger" @click="openEdit()">新建</a-button>
                </a-form-item>
            </a-form>
        </template>
        <a-table bordered :columns="columns" :rowKey="record => record.id" :dataSource="dataSource"
                 :pagination="pagination"
                 :loading="loading" @change="handleTableChange">


            <template slot="avatar" slot-scope="text, record">


                    <a-avatar   class="exAvatar"  :src="text">{{ record.name.substr(0, 1) }}</a-avatar>
            </template>


            <!-- 状态插槽 -->
            <template slot="status" slot-scope="text, record">
                <a-tag :color=" text==='1'?'blue':''  ">{{ $exMod.findDict(OperatorStatusOPT, text) }}</a-tag>
            </template>
            <!-- 操作插槽 -->
            <template slot="operation" slot-scope="text, record">

                <a-popconfirm :title='`确定要重置密码为 ${rePassWord} 吗?`' @confirm="() => resetPassword(record)">
                    <span><a href="javascript:void(0) ;">重置密码</a></span>
                </a-popconfirm>

                <a-divider type="vertical"/>


                <a @click="openEdit(record)">编辑</a>
                <a-divider type="vertical"/>
                <a-popconfirm :title="'确定要删除操作员 '+record.name+' 吗?'" @confirm="() => deleteOperator(record)">
                    <span><a href="javascript:void(0);">删除</a></span>
                </a-popconfirm>
            </template>
        </a-table>

        <EditBox v-if="editShow" @closeFun="closeEdit" :editRecord="editRecord"/>
    </a-card>
</template>
<script>
    import { dictLoad } from '../../../util/internal.api';
    import {pagination, operatorTable} from '../public/colums';
    import {operatorList, operatorDel, operatorUpdate} from '../public/http';

    export default {
        components: {
            EditBox: resolve => {
                require(['./edit.vue'], resolve);
            }
        },
        data() {
            return {
                editShow: false, //打开编辑器
                editRecord: {}, //正在编辑的记录
                dataSource: [],
                pagination: {
                    ...pagination
                }, //复制预设的分页参数
                loading: false, //表格loading
                columns: operatorTable, //表头管理
                searchForm: {
                    name: '',
                    account: '',
                    phone: '',
                    email: ''
                },
                OperatorStatusOPT: [],
                rePassWord: "123456",
            };
        },
        methods: {
            //1.0 获取表格数据
            getTableList() {
                this.loading = true;
                let searchData = this.$exMod.filterObjEmpty(this.searchForm); //删除对象中的空值
                operatorList(this.pagination, searchData).then(res => {
                    this.loading = false;
                    if (res.code === 0) {
                        this.dataSource = res.data.result; //表格数据
                        this.pagination.total = res.data.total; //总记录条数
                    }
                });
            },
            //1.2翻页监听
            handleTableChange(pagination) {
                this.pagination.current = pagination.current;
                this.pagination.pageSize = pagination.pageSize;
                this.getTableList();
            },
            //2.0打开编辑器
            openEdit(obj = {}) {
                this.editRecord = obj;
                this.editShow = true;
            },
            //2.1 关闭编辑器
            closeEdit(e) {
                this.editShow = false;
                if (e.render) {
                    this.getTableList();
                }
            },
            //3.0 搜索提交
            searchSubmit() {
                this.pagination.current = 1;
                this.getTableList();
            },
            //4.0删除操作员
            deleteOperator(record) {
                this.loading = true;
                operatorDel(record.id).then(res => {
                    if (res.code === 0) {
                        //成功提醒
                        this.$notification.success({
                            message: '操作成功',
                            description: res.message
                        });
                        this.getTableList();
                    } else {
                        //错误提醒
                        this.$notification.warning(res.message);
                    }
                    this.loading = false;
                });
            },

            //5.0 重置密码
            resetPassword(record) {
                let data = {id: record.id, password: this.rePassWord}
                operatorUpdate(record.id, data).then(res => {
                    //成功提醒
                    this.$notification.success({
                        message: '操作成功',
                        description: res.message
                    });
                });

            },
            //数据初始化
            async init() {
                await dictLoad('OperatorStatus').then(res => (this.OperatorStatusOPT = res));
                this.getTableList();
            }

        },
        created() {
            this.init();
        }
    };
</script>
